﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
//该源码下载自www.51aspx.com(５１ａｓpｘ．cｏｍ)

public partial class Gdzc : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            
            SqlConnection conn = DB.GetCon();
            conn.Open();
            AutoID(conn);//自动编号
            this.bind();
            this.Panel1.Visible = false;
            DataSet ds = DB.reDs("SELECT  Title FROM tb_BigType where Type='物资大类'");
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                //显示数据库中的类别并给TreeView根节点
                TreeNode tree1 = new TreeNode(ds.Tables[0].Rows[i][0].ToString());
                this.TreeView1.Nodes.Add(tree1);
                //tree1.Expand();
                DataSet ds1 = DB.reDs("select title from tb_Type Where type='" + ds.Tables[0].Rows[i][0].ToString() + "'");
                //显示TreeView根节点下的子节点
                for (int j = 0; j < ds1.Tables[0].Rows.Count; j++)
                {
                    TreeNode tree2 = new TreeNode(ds1.Tables[0].Rows[j][0].ToString(), ds1.Tables[0].Rows[j][0].ToString());
                    tree1.ChildNodes.Add(tree2);
                }
            }
        }
    }
    //根据数据库中的记录数自动产生一个数作为入库单ID
    public void AutoID(SqlConnection conn)
    {
        SqlCommand com = new SqlCommand("select count(*) from tb_Gdzc", conn);
        int Count = Convert.ToInt32(com.ExecuteScalar());
        this.txtGdzcID.Text = Convert.ToString(Count + 1);
        if (conn.State == ConnectionState.Open)
        {
            com.Dispose();
            conn.Close();
        }
    }
    public void bind()
    {
        DataSet ds1 = DB.reDs("select Title from tb_BigType where Type='仓库'");
        this.ddlStorePlace.DataSource = ds1;
        ddlStorePlace.DataTextField = "Title";
        ddlStorePlace.DataBind();
        DataSet ds2 = DB.reDs("select Title from tb_BigType where type='部门'");
        this.ddlDepartment.DataSource = ds2;
        ddlDepartment.DataTextField = "Title";
        ddlDepartment.DataBind();
        this.ddlBuyDepartment.DataSource = ds2;
        ddlBuyDepartment.DataTextField = "Title";
        this.ddlBuyDepartment.DataBind();
        DataSet ds3=DB.reDs("select * from tb_Enterprise  where type1='是'");
        this.ddlCustomer.DataSource = ds3;
        ddlCustomer.DataTextField = "Name";
        ddlCustomer.DataValueField = "CustomerID";
        ddlCustomer.DataBind();
    }
    //使页面恢复初始状态
    public void clear()
    {
        foreach (Control c in this.form1.Controls)//遍历控件查找文本框控件，并清空
        {
            if (c.GetType().ToString() == "System.Web.UI.WebControls.TextBox")
            {
                TextBox t = (TextBox)c;
                t.Text = "";
            }
        }
        foreach (Control c in this.form1.Controls)//遍历控件查找DropDownList控件，并恢复默认选项
        {
            if (c.GetType().ToString() == "System.Web.UI.WebControls.DropDownList")
            {
                DropDownList d = (DropDownList)c;
                d.SelectedIndex = -1;
            }
        }
    }
    protected void btnClick_Click(object sender, EventArgs e)
    {
        string GdzcID = this.txtGdzcID.Text.Trim();
        string WzID = this.lnkWzID.Text;
        string BuyDate = this.txtBuyDate.Text.Trim();
        string BuyDepartment = this.ddlBuyDepartment.SelectedValue.ToString();
        string BuyMan = this.txtBuyMan.Text.Trim();
        string StorePlace = this.ddlStorePlace.SelectedValue.ToString();
        string Department = this.ddlDepartment.SelectedValue.ToString();
        string User = this.txtUsers.Text.Trim();

        string Name = this.txtName.Text.Trim();
        string Model = this.txtModel.Text.Trim();
        string Spec = this.txtSpec.Text.Trim();
        string Card = this.txtCard.Text.Trim();
        string UnitPrise = this.txtUnitPrice.Text.Trim();
        string StoreDate = this.txtStoreDate.Text.Trim();
        string Repair = this.txtRepair.Text.Trim();
        string State = this.ddlState.SelectedValue.ToString();
        string CustomerID = this.ddlCustomer.SelectedValue.ToString();
        string Customer = this.ddlCustomer.SelectedItem.Text;
        string Address = this.txtPlace.Text.Trim();
        string LinkMan = this.txtLinkMan.Text.Trim();
        string Phont = this.txtPhone.Text.Trim();
        try
        {
            bool b=DB.ExSql("insert into tb_Gdzc values('" + GdzcID + "' ,'" + WzID + "', '" + BuyDate + "', '" + BuyDepartment + "', '" + BuyMan + "', '" + StorePlace + "','" + Department + "', '" + User + "')");
            if(b)
                DB.ExSql("insert into tb_GdzcDetails values('" + GdzcID + "','" + WzID + "','" + Name + "','" + Model + "','" + Spec + "','" + Card + "','" + UnitPrise + "','" + StoreDate + "','" + Repair + "','" + State + "','" + CustomerID + "','" + Customer + "','" + Address + "','" + LinkMan + "','" + Phont + "') ");
            //使页面恢复初始状态
            this.clear();
            SqlConnection conn = DB.GetCon();
            conn.Open();
            AutoID(conn);//根据数据库中的记录数自动产生一个数作为入库单ID
            this.lnkWzID.Text = "物资编号";
        }
        catch(SqlException oe)
        {
            Response.Write(oe.Message.ToString());
        }
    }
    protected void lnkWzID_Click(object sender, EventArgs e)
    {
        this.Panel1.Visible = true;

    }
    protected void gvStatistic_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        this.Panel1.Visible = false;
        string id = this.gvStatistic.DataKeys[e.NewSelectedIndex].Value.ToString();
        DataSet ds = DB.reDs("select * from tb_Material where WzID='" + id + "'");
        this.lnkWzID.Text=ds.Tables[0].Rows[0][0].ToString();
        this.txtName.Text=ds.Tables[0].Rows[0][2].ToString();
        this.txtModel.Text=ds.Tables[0].Rows[0][3].ToString();
        this.txtSpec.Text=ds.Tables[0].Rows[0][4].ToString();
        this.txtCard.Text=ds.Tables[0].Rows[0][5].ToString();
        this.txtUnitPrice.Text = ds.Tables[0].Rows[0][6].ToString();
    }
    protected void TreeView1_SelectedNodeChanged1(object sender, EventArgs e)
    {
        DataSet ds = DB.reDs("select * from tb_Material where WzType='" + this.TreeView1.SelectedNode.Value.ToString() + "'");
        this.gvStatistic.DataSource = ds;
        this.gvStatistic.DataKeyNames = new string[] { "WzID" };
        this.gvStatistic.DataBind();
    }
    protected void ddlCustomer_SelectedIndexChanged(object sender, EventArgs e)
    {
        string CustomerID = this.ddlCustomer.SelectedValue.ToString();
        DataSet ds = DB.reDs("select * from tb_Enterprise  where CustomerID='" + CustomerID + "'");
        this.txtPlace.Text = ds.Tables[0].Rows[0][2].ToString();
        this.txtPhone.Text = ds.Tables[0].Rows[0][3].ToString();
        this.txtLinkMan.Text = ds.Tables[0].Rows[0][4].ToString();
    }
}
